#pragma once
#include <AVUIFrameworkElement.h>

namespace AVUI {

class FormattedText;
class CaretElement;
class TextBox;

class TextBoxView : public FrameworkElement
{
    friend class TextBox;
public:
    DECLARE_ELEMENT(TextBoxView, FrameworkElement);

    DECLARE_DEPENDENCY_PROPERTY_BEGIN(Text);
        DECLARE_ATTRIBUTE_CONTENTPROPERTY();
    DECLARE_DEPENDENCY_PROPERTY_END();
    TRefCountedPtr<String> get_Text() { return (String*)(Object*)GetValue(get_TextProperty()); }
    void set_Text(String* pValue) { SetValue(get_TextProperty(), (Object*)pValue); } 

    DECLARE_DEPENDENCY_PROPERTY(Foreground);
    TRefCountedPtr<Brush> get_Foreground() { return (Brush*)(Object*)GetValue(get_ForegroundProperty()); }
    void set_Foreground(Brush* pValue) { SetValue(get_ForegroundProperty(), (Object*)pValue); } 

    DECLARE_DEPENDENCY_PROPERTY(FontFamily);
    String* get_FontFamily() { return object_cast<String>(GetValue(get_FontFamilyProperty())); }
    void set_FontFamily(String* pFontFamily) { SetValue(get_FontFamilyProperty(), pFontFamily); }

    DECLARE_DEPENDENCY_PROPERTY(FontSize);
    float get_FontSize() { return AVUI::UnboxValue<float>(GetValue(get_FontSizeProperty())); }
    void set_FontSize(float value) { SetValue(get_FontSizeProperty(), AVUI::BoxValue(value)); } 

    TextBoxView();
    virtual ~TextBoxView();


protected:

    virtual int get_VisualChildrenCount();
    virtual Visual* GetVisualChild(int idx);

private:
    static void StaticConstructor();

    void set_TextBox(TextBox* pTextBox) { m_pTextBox = pTextBox; }
    void ShowCaret();
    void HideCaret();
    void set_CaretIndex(int index);
    int get_CaretIndexFromPoint(const Point& point);

    Size MeasureOverride(const Size& constraintSize);
    Size ArrangeOverride(const Size& constraintSize);
    void OnRender(DrawingContext& drawingContext);

    TRefCountedPtr<FormattedText> m_pFormattedText;
    bool m_isCaretVisible;
    TRefCountedPtr<CaretElement> m_pCaretElement;
    int m_caretIndex;
    TextBox* m_pTextBox;
};


}; // namespace AVUI
